<template>
  <div>
    <f-form ref='form' :action='api.payment' :form-key='this.formItem.paymentId' :handlerName='handlerName'>
      <div>
        <f-form-item label='结算对象' name='saleManName' disabled :required='true' :value='formItem.saleManName' />
        <f-form-item label='结算金额' name='amount' disabled data-type='number' :required='true' :value.sync='formItem.amount' />
        <f-form-item label='备注' name='remark' :value='formItem.remark' />
      </div>
    </f-form>
    <select-sale-man-bank-modal ref='saleManBankModal' @onSelectChange='onSelectSaleManBankChange'></select-sale-man-bank-modal>
  </div>
</template>
<script>

import api from "../../../js/api"
import SelectInvoiceModal from "@/views/module/haiying/components/select_invoice_modal"
import SelectSaleManBankModal from "@/views/module/haiying/components/select_saleman_bank_modal"

export default {
  name: "SettlePay",
  components: { SelectSaleManBankModal, SelectInvoiceModal },
  data() {
    let me = this
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      loaded: false,
      handlerName: "hySettlePayPaymentSaveHandler",
      saleManName: null,
      selectIds: "",
      formItem: this.getFormItem()
    }
  },
  created() {
  },
  computed: {},
  methods: {
    getFormItem() {
      return {
        deptSettleInvoiceId: "",
        settleType: 1,
        saleManName: null,
        amountDate: null,
        totalSettleAmount: 0,
        remark: ""
      }
    },
    setData(data) {
      if (data.selectIds) {
        this.$refs.form.resetFields()
        this.formItem = this.getFormItem()
        this.selectIds = data.selectIds
        this.getSettlePayInfo()
      }
    },
    /**
     * 获取支付信息
     */
    getSettlePayInfo() {
      this.http.get(this, {
        url: this.api.payment.getSettlePayInfo,
        params: {
          settleIds: this.selectIds
        },
        success: (me, data) => {
          this.$nextTick(() => {
            this.formItem = data
            if (data.totalSettleAmount) {
              this.formItem.saleManName = data.saleManName
              this.formItem.saleManId = data.saleManId
              this.formItem.deptId = data.deptId
              this.formItem.settleApplyId = data.settleApplyId
              this.$refs.form.getForm().setFieldsValue({ "saleManName": data.saleManName })
              this.$refs.form.getForm().setFieldsValue({ "amount": data.totalSettleAmount })
            } else {
              this.formItem.saleManId = ""
              this.formItem.deptId = ""
              this.formItem.settleApplyId = ""
              this.$refs.form.getForm().setFieldsValue({ "saleManName": "" })
              this.$refs.form.getForm().setFieldsValue({ "amount": null })
              this.formItem.totalSettleAmount = null
            }
            this.$forceUpdate()
          })
        }
      })
    },
    /**
     * 结算范围选择
     */
    settleTypeChange(val) {
      this.formItem.settleType = val
      this.getSettleApplyInfo()
    },
    /**
     * 选择收款账户
     */
    openSaleManBankModal() {
      this.$refs["saleManBankModal"].open({
        saleManId: this.formItem.saleManId
      })
    },
    /**
     * 收款账户选择回调
     */
    onSelectSaleManBankChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "bankName": selectedRows[0].bankName })
      this.formItem.bankId = selectedRowKeys[0]
      this.$forceUpdate()
    },
    handleSubmit() {
      this.$refs["form"].submit({
        selectIds: this.selectIds,
        saleManId: this.formItem.saleManId,
        deptId: this.formItem.deptId,
        settleApplyId: this.formItem.settleApplyId
      }, this.handleSuccess)
    },
    handleSuccess() {
      this.$emit("closeDrawer", false)
    }
  }
}
</script>
